# -*- coding:utf-8 -*-

from django.contrib import admin
from manualpick.models import TMobilePick, TWechatUser
from callcentre.models import TMobilePicking


@admin.register(TWechatUser)
class TWechatUserAdmin(admin.ModelAdmin):
    list_display = ('username', 'nickname', 'note', 'status',)
    list_filter = ('status',)
    list_per_page = 20
    search_fields = ('username', 'nickname',)

    def save_model(self, request, obj, form, change):
        def get_ip():
            """获取客户端的IP地址"""
            if 'HTTP_X_FORWARDED_FOR' in request.META:
                return request.META['HTTP_X_FORWARDED_FOR']
            return request.META['REMOTE_ADDR']

        if not change:  # 新增数据时
            user_obj = request.user
            obj.create_user = user_obj.username
            obj.client_ip = get_ip()
        obj.save()

    def get_queryset(self, request):
        qs = super().get_queryset(request)

        if request.user.is_superuser:
            return qs
        return qs.filter(create_user=request.user.username)


@admin.register(TMobilePicking)
class TMobilePickingAdmin(admin.ModelAdmin):
    import_selected_confirmation_template = None

    list_display = ('mobile', 'name', 'user_name')
    search_fields = ('mobile', 'name', 'user_name',)
    list_filter = ('create_user',)

    def has_delete_permission(self, request, obj=None):
        return False

    def delete_model(self, request, obj):
        pass

    def get_queryset(self, request):
        from django.contrib.auth.models import User

        qs = super().get_queryset(request)

        user = request.user
        groups = user.groups.all().values_list('pk', flat=True)
        users = User.objects.filter(groups__pk__in=groups).values_list('username', flat=True).distinct()
        qs = qs.filter(create_user__in=users)
        return qs.filter(status=TMobilePicking.INIT)

    def get_urls(self):
        from django.urls import path
        from django.views.generic import RedirectView
        from functools import update_wrapper

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)

            wrapper.model_admin = self
            return update_wrapper(wrapper, view)

        info = self.model._meta.app_label, self.model._meta.model_name


        urlpatterns = super().get_urls()
        return urlpatterns

    def get_actions(self, request):
        from .actions import _with_improt_wechat
        actions = super().get_actions(request)

        actions.pop('delete_selected', None)

        wechat_users = TWechatUser.objects.filter(create_user=request.user.username).filter(status=TWechatUser.ENABLED)

        for wechat_user in wechat_users:
            action = _with_improt_wechat(wechat_user.username, wechat_user.nickname)
            actions.setdefault(action.__name__, self.get_action(action))

        return actions


@admin.register(TMobilePick)
class TMobilePickAdmin(admin.ModelAdmin):
    list_display = ('mobile', 'wechat', 'status', 'mtime',)
    fields = ('mobile', 'wechat', 'note', 'status',)
    list_filter = ('status',)
    search_fields = ('mobile', 'wechat', 'note',)
    list_editable = ('wechat', 'status',)
    list_per_page = 20
    ordering = ('-pk',)

    # radio_fields = {'status': admin.VERTICAL}

    actions = None

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.filter(create_user=request.user.username)

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        if db_field.name == 'wechat':
            username = request.user.username
            qs = TWechatUser.objects.filter(create_user=username).values_list('username', 'nickname')
            db_field.choices = list(qs)
        return super().formfield_for_dbfield(db_field, request, **kwargs)
